import { computed } from "vue"
export default function useItemFocus(data, callback) {
	const focusItem = computed(() => {
		let focusArr = []
		let unFocusArr = []
		data.value.blocks.forEach((item) => (item.focus ? focusArr : unFocusArr).push(item))
		return { focusArr, unFocusArr }
	})
	let lastFocusItem = computed(
	{
		get(){
			let lastFocusItem = null
			data.value.blocks.forEach((item) => {
				if (item.focus) {
					lastFocusItem = item
				}
			})
			return lastFocusItem
		},
		set(value){
			data.value.blocks.forEach((item) => {
				if (item.focus) {
					lastFocusItem.value = value
				}
			})
		}
	}
)
	const clearAllFocus = () => {
		data.value.blocks.forEach((item) => {
			item.focus = false
		})
	}
	//获取焦点
	const mousedown = (e, item) => {
		e.preventDefault()
		e.stopPropagation()
		if (e.shiftKey) {
			if (focusItem.value.focusArr.length == 1 && item.focus) {
			} else {
				item.focus = !item.focus
			}
		} else {
			clearAllFocus()
			item.focus = !item.focus
		}
		callback(e)
	}

	return {
		focusItem,
		clearAllFocus,
		mousedown,
		lastFocusItem
	}
}
